﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;
using WorldMapNew;

public class TrafficRoadPoint
{
    public List<GameObject> roadPoints;
}

public class TrafficPoint : MonoBehaviour
{
    public bool isRoadPoint = false;
    public int pointId = 0;
    public float drawScale = 1f; // 绘制缩放大小

    SphereCollider _box;
#if UNITY_EDITOR
    [DrawGizmo(GizmoType.Pickable)]
    void OnDrawGizmos()
    {
        if (pointId == 0) // 该点没有连接到节点
        {
            Gizmos.color = Color.white;
            Gizmos.DrawSphere(transform.position, 0.1f);
        }
        else
        {
            if (isRoadPoint == false)
            {
                Gizmos.color = Color.blue;
                Gizmos.DrawSphere(transform.position, 0.06f * drawScale);
                if(_box) _box.radius = 0.06f * drawScale;
            }
            else
            {
                Gizmos.color = Color.red;
                Gizmos.DrawSphere(transform.position, 0.06f * drawScale);
            }
        }
    }
#endif
    public void SetPointId(int id)
    {
        pointId = id;
        if (id > 0 && isRoadPoint == false)
        {
            if (_box == null)
            {
                _box = gameObject.AddComponent<SphereCollider>();
                _box.radius = 0.2f;
            }
        }
        if (isRoadPoint)
        {
            gameObject.name = "RoadPoint" + id;
        }
        else
        {
            gameObject.name = "Point_" + id;
        }

        if (id <= 0 && _box)
        {
            _box.enabled = false;
            gameObject.name = "Point";
        }
    }

    public void SetPointDrawSize(float scale)
    {
        drawScale = scale;
    }

    public List<TrafficRoadPointData> GetRoadPointData()
    {
        List<TrafficRoadPointData> datas = new List<TrafficRoadPointData>();
        // 获得所有子节点
        for (int i = 0; i < transform.childCount; i++)
        {
            Transform tf = transform.GetChild(i);
            int pointId = 0;
            int.TryParse(tf.gameObject.name, out pointId);
            if (pointId > 0)
            {
                TrafficRoadPointData data = new TrafficRoadPointData();
                data.lineId = pointId;
                data.roadPoints = new List<Vector3>();
                for (int ii = 0; ii < tf.childCount; ii++)
                {
                    Transform roadTf = tf.GetChild(ii);
                    data.roadPoints.Add(roadTf.position);
                }
                datas.Add(data);
            }
        }
        return datas;
    }
}
